package org.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zhxy.model.entity.Grade;
import org.zhxy.model.result.Result;
import org.zhxy.service.GradeService;

import java.util.List;

@Tag(name = "年级管理")
@Slf4j
@RestController
@RequestMapping("/sms/gradeController")
@RequiredArgsConstructor
public class GradeController {
    private final GradeService gradeService;

    @Operation(summary = "分页条件查询年级列表")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result<IPage<Grade>> page(@PathVariable int pageNo, @PathVariable int pageSize, @RequestParam(required = false) String gradeName) {
        log.info("分页条件查询年级列表，请求参数：{},{},{}", pageNo, pageSize, gradeName);

        IPage<Grade> page = new Page<>(pageNo, pageSize);
        gradeService.page(page, gradeName);
        log.info("分页条件查询年级列表，响应数据：{}", page);

        return Result.ok(page);
    }

    @Operation(summary = "保存或更新年级信息")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@RequestBody Grade grade) {
        log.info("保存或更新年级信息，请求参数：{}", grade);

        gradeService.saveOrUpdate(grade);

        return Result.ok();
    }

    @Operation(summary = "批量删除年级信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGradeById(@RequestBody List<Integer> ids) {
        log.info("批量删除年级信息，请求参数：{}", ids);

        gradeService.removeByIds(ids);

        return Result.ok();
    }

    @Operation(summary = "查询所有年级信息")
    @GetMapping("/getGrades")
    public Result getGrades() {
        List<Grade> grades = gradeService.list();
        log.info("查询所有年级信息，响应数据：{}", grades);

        return Result.ok(grades);
    }
}
